﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace ERP.UI.SystemAdministrator
{
	public partial class LoginSettings
	{
		public LoginSettings()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (CheckFields())
            {
            }
        }

        private bool CheckFields()
        {
            bool result = false ;
            if (!txtNewUsername.Text.Equals(string.Empty))
            {
                //user wants to change the current username
                if (txtOldUsername.Text.Equals(ApplicationVariables.CurrentUser.Username))
                {
                    //correct old username
                    if (txtNewUsername.Text.Equals(txtConfirmNewUsername.Text))
                    {
                        //new username confirmed
                        result = CheckPassword();
                    }
                    else
                        result = false;
                }
                else
                    result = false;
            }
            else 
            {
                result = CheckPassword();
            }       

            return result;
        }

        private bool CheckPassword()
        {
            bool result = false;
            if (!txtNewPassword.Password.Equals(string.Empty))
            {
                if (txtOldPassword.Password.Equals(ApplicationVariables.CurrentUser.Password))
                {
                    if (txtConfirmNewPassword.Password.Equals(txtNewPassword.Password))
                        result = true;
                }
            }
            else if (!txtNewUsername.Text.Equals(string.Empty))//when username changes password must be reset
                result = false;
            return result;
        }
	}
}